<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class General_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
    public function getDesc() {
        $query = $this->db->query("
            SELECT barangay_description AS description FROM tb_barangays
            WHERE barangay_id_pk=".$this->session->userdata('barangay_id'));
        return $query->result();
    }
    public function signUp() {
        $prov = $this->input->post('prov_id');
        $mun = $this->input->post('mun_id');
        $brgy = $this->input->post('brgy_name');
        $con = $this->input->post('con_p');
        $email = $this->input->post('e_ad');
        $phone = $this->input->post('p_num');
        $check_brgy = $this->db->query("
            SELECT 'x' FROM tb_barangays
                WHERE barangay_name='".$brgy."' AND
                barangay_municipality_id_fk=".$mun." AND
                barangay_province_id_fk=".$prov."
        ");
        if(count($check_brgy->result()) != 0) {
            return "nok";
        } else {
            $add_brgy = $this->db->query("
                INSERT INTO  tb_barangays (
                    barangay_id_pk,
                    barangay_municipality_id_fk,
                    barangay_province_id_fk,
                    barangay_name,
                    barangay_person,
                    barangay_email,
                    barangay_phone,
                    barangay_status,
                    barangay_created
                    )
                    VALUES (
                    ".date('symHid').rand(1000000, 9999999)." ,  '".$mun."',  '".$prov."',  '".$brgy."', '".$con."',  '".$email."', '".$phone."', '3', now()
                    )
            ");
            return $this->db->insert_id();
        }
    }
    public function update() {
        $prov = $this->input->post('prov_id');
        $mun = $this->input->post('mun_id');
        $brgy = $this->input->post('brgy_name');
        $zip = $this->input->post('zip');
        $email = $this->input->post('e_ad');
        $phone = $this->input->post('p_num');
        $query = $this->db->query("
            UPDATE  tb_barangays SET  barangay_name =  '".$brgy."',
            barangay_municipality_id_fk =  '".$mun."',
            barangay_province_id_fk =  '".$prov."',
            barangay_zip =  '".$zip."',
            barangay_email =  '".$email."',
            barangay_phone =  '".$phone."' 
            WHERE  `tb_barangays`.`barangay_id_pk` = ".$this->session->userdata('barangay_id')."
            LIMIT 1
        ");
    }
    public function changeDesc() {
        $brgy_desc = $this->input->post('brgy_desc');
        $query = $this->db->query("
            UPDATE  tb_barangays SET  barangay_description =  '".$brgy_desc."' 
                WHERE  barangay_id_pk =".$this->session->userdata('barangay_id')." 
                LIMIT 1
        ");
    }
    public function getProvince() {
        $query = $this->db->query("
            SELECT * FROM tb_provinces
            ORDER BY province_name
        ");
        return $query->result();
    }
    public function getMunicipality($p_id) {
        $query = $this->db->query("
            SELECT * FROM tb_municipalities
            WHERE municipality_province_id_fk="
            .$p_id."
            ORDER BY municipality_name
        ");
        return $query->result();
    }
    public function getBarangay($m_id) {
        $query = $this->db->query("
            SELECT * FROM tb_barangays
            WHERE barangay_municipality_id_fk="
            .$m_id."
            AND barangay_status=2
            ORDER BY barangay_name
        ");
        return $query->result();
    }
    public function getAllPlain() {
        
    }
    public function newlyApprovedBarangays() {
        $query = $this->db->query("
            SELECT a.barangay_id_pk AS brgy_id
                , a.barangay_name AS brgy_name
                , b.municipality_name AS mun_name
                , c.province_name AS prov_name
            FROM tb_barangays a
                , tb_municipalities b
                , tb_provinces c
            WHERE 
                a.barangay_municipality_id_fk = b.municipality_id_pk
            AND a.barangay_province_id_fk = c.province_id_pk
            ORDER BY a.barangay_approved DESC 
            LIMIT 8");
        return $query->result();
    }
}